package com.ifar.audiosdk.base

import android.view.SurfaceView
import android.view.ViewGroup
import com.ifar.audiosdk.IfarBaseApp
import com.ifar.audiosdk.common.IfarVideoConfig
import com.ifar.audiosdk.model.engine.CurrentUserSettings
import com.ifar.audiosdk.model.engine.EngineConstant
import io.agora.rtc.RtcEngine
import io.agora.rtc.video.VideoCanvas
import io.agora.rtc.video.VideoEncoderConfiguration
import io.agora.rtc.video.VideoEncoderConfiguration.VideoDimensions

/**
 * 带视频功能的baseActivity
 */
abstract class EngineBaseActivity<VM : IfarBaseViewModel> : BaseActivity<VM>() {
    protected val mVideos = mutableMapOf<Int, SurfaceView>()  //视频流集合

    protected abstract fun deInitUIandEvent()

    override fun onDestroy() {
        deInitUIandEvent()
        super.onDestroy()
    }

    protected open fun application(): IfarBaseApp {
        return application as IfarBaseApp
    }

    protected open fun rtcEngine(): RtcEngine {
        return application().rtcEngine()
    }

    /**个人通话设置，暂未使用*/
    protected open fun vSettings(): CurrentUserSettings {
        return application().userSettings()
    }

    /**本地预览*/
    protected open fun preview(start: Boolean, view: SurfaceView, uid: Int) {
        if (start) {
            rtcEngine().setupLocalVideo(VideoCanvas(view, VideoCanvas.RENDER_MODE_HIDDEN, uid))
            rtcEngine().startPreview()
        } else {
            rtcEngine().stopPreview()
        }
    }

    /**加入频道*/
    protected open fun joinChannel(channel: String, uid: Int, token: String) {
        rtcEngine().joinChannel(null, channel, "OpenLive", uid)
        enablePreProcessor()
    }

    /**离开频道*/
    protected open fun leaveChannel() {
        disablePreProcessor()
        rtcEngine().leaveChannel()
    }

    /**开启美颜*/
    protected open fun enablePreProcessor() {
        rtcEngine().setBeautyEffectOptions(true, EngineConstant.BEAUTY_OPTIONS)
    }

    /**关闭美颜*/
    protected open fun disablePreProcessor() {
        rtcEngine().setBeautyEffectOptions(false, EngineConstant.BEAUTY_OPTIONS)
    }

    /**本地输出的视频流的配置*/
    protected open fun configEngine() {
        rtcEngine().setVideoEncoderConfiguration(
            VideoEncoderConfiguration(
                VideoDimensions(IfarVideoConfig.resolutionW, IfarVideoConfig.resolutionH),
                IfarVideoConfig.VIDEO_FPS[if (IfarVideoConfig.FPS == IfarVideoConfig.FPS_15) 0 else 1],
                VideoEncoderConfiguration.STANDARD_BITRATE,
                VideoEncoderConfiguration.ORIENTATION_MODE.ORIENTATION_MODE_ADAPTIVE
            )
        )
    }

    private fun surfaceView(): SurfaceView {
        val surfaceV = RtcEngine.CreateRendererView(this)
        surfaceV.setZOrderOnTop(false)
        surfaceV.setZOrderMediaOverlay(false)
        return surfaceV
    }

    /**本地视频流加载*/
    protected open fun saveLocalVideo(uid: Int, localView: ViewGroup) {
        val surfaceV = surfaceView()
        preview(true, surfaceV, uid)
        mVideos[uid] = surfaceV
    }

    /**远端视频流加载*/
    protected open fun saveRemoteVideo(uid: Int) {
        val surfaceV = surfaceView()
        rtcEngine().setupRemoteVideo(
            VideoCanvas(surfaceV, VideoCanvas.RENDER_MODE_HIDDEN, uid)
        )
        mVideos[uid] = surfaceV
    }

    /**填充视频流*/
    protected open fun loadVideo(uid: Int, newParent: ViewGroup) {
        val oldParent = mVideos[uid]?.parent as ViewGroup?
        oldParent?.removeAllViews()
        newParent.removeAllViews()
        newParent.addView(mVideos[uid])
    }
}